package cn.itcast.nems.approval.service.impl;

import cn.itcast.nems.approval.dao.entity.ApprovalAdditionalForm;
import cn.itcast.nems.approval.dao.mapper.ApprovalAdditionalFormMapper;
import cn.itcast.nems.approval.service.ApprovalAdditionalFormService;
import cn.itcast.nems.approval.service.ApprovalFormService;
import cn.itcast.nems.common.account.AccountHolder;
import cn.itcast.nems.common.entity.BaseEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 审批单补充信息表单 服务实现类
 * </p>
 *
 * @author czq
 * @since 2024-03-08
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ApprovalAdditionalFormServiceImpl extends ServiceImpl<ApprovalAdditionalFormMapper, ApprovalAdditionalForm> implements ApprovalAdditionalFormService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String create(ApprovalAdditionalForm params) {
        int insert = baseMapper.insert(params);
        if (insert == 1) {
            return params.getId();
        }
        return null;
    }

    @Override
    public List<String> findBpmNodeKeysByFormId(String formID, ApprovalFormService.JournalType journalType) {
        List<ApprovalAdditionalForm> list = lambdaQuery().eq(ApprovalAdditionalForm::getFormId, formID).
                eq(ApprovalAdditionalForm::getJournalType, journalType.name())
                .eq(ApprovalAdditionalForm::getDeletedFlag, false)
                .groupBy(ApprovalAdditionalForm::getBpmNodeKey).list();
        return list.stream().map(ApprovalAdditionalForm::getBpmNodeKey).collect(Collectors.toList());
    }

    @Override
    public Boolean checkFormFlag(String formID, List<String> bpmNodeKeys, ApprovalFormService.JournalType journalType) {
        return lambdaQuery().eq(ApprovalAdditionalForm::getFormId, formID)
                .in(ApprovalAdditionalForm::getBpmNodeKey, bpmNodeKeys)
                .eq(ApprovalAdditionalForm::getJournalType, journalType.name())
                .eq(ApprovalAdditionalForm::getDeletedFlag, false)
                .groupBy(ApprovalAdditionalForm::getBpmNodeKey).exists();
    }


    @Override
    public void delete(String formID) {
        lambdaUpdate().eq(ApprovalAdditionalForm::getFormId, formID)
                .set(ApprovalAdditionalForm::getDeletedBy, AccountHolder.getRequiredAccount())
                .set(ApprovalAdditionalForm::getDeletedDatetime, LocalDateTime.now())
                .set(ApprovalAdditionalForm::getDeletedFlag, Boolean.TRUE).update();
    }

    @Override
    public Map<String, Object> formAdditionalFroms(String formId) {
        Map<String, Object> result = new HashMap<>();
        var values = baseMapper.selectList(new QueryWrapper<ApprovalAdditionalForm>()
                        .lambda()
                        .eq(ApprovalAdditionalForm::getFormId, formId)
                        .eq(ApprovalAdditionalForm::getDeletedFlag, false))
                .stream()
                .sorted(Comparator.comparing(BaseEntity::getCreatedDatetime))
                .toList();
        for (final var f : values) {
            result.put(f.getFieldKey(), f.getFieldValue());
        }
        return result;
    }
}
